package com.test.mchelepesa.activity_fragment_rice.fragment_rice;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static com.test.mchelepesa.activity_fragment_rice.loan_rice.LoanMainActivity_rice.repayNoticeFlag;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.test.mchelepesa.activity_fragment_rice.base_rice.BaseLoanFragment_rice;
import com.test.mchelepesa.bean_rice.StaticConfig_rice;
import com.test.mchelepesa.bean_rice.config_info_rice.AppShowInfo_rice;
import com.test.mchelepesa.bean_rice.user_info_rice.RepayInfo_rice;
import com.test.mchelepesa.constant_rice.Constant_rice;
import com.test.mchelepesa.dialog_rice.NoticeDialog_rice;
import com.test.mchelepesa.util_rice.StringUtil_rice;
import com.test.mchelepesa.util_rice.http_rice.HttpManager_rice;
import com.test.mchelepesa.R;


public class RepayFragment_rice extends BaseLoanFragment_rice {

    private TextView totalRepaymentTv, dueDateTv;
    private TextView loanAmountTv, interestTv, serviceFeeTv, overdueDaysTv, overduePenaltyTv;
    private View serviceFeeView,serviceFeeLine;

    public boolean isOverdue = false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_repay_rice, container, false);
        init(view);
        return view;
    }

    private void init(View view){

        totalRepaymentTv = view.findViewById(R.id.fragment_repay_tv_total_repayment);
        dueDateTv = view.findViewById(R.id.fragment_repay_tv_due_date);
        loanAmountTv = view.findViewById(R.id.fragment_repay_tv_loan_amount);
        interestTv = view.findViewById(R.id.fragment_repay_tv_interest);
        serviceFeeTv = view.findViewById(R.id.fragment_repay_tv_service_fee);
        overdueDaysTv = view.findViewById(R.id.fragment_repay_tv_overdue_days);
        overduePenaltyTv = view.findViewById(R.id.fragment_repay_tv_overdue_penalty);
        serviceFeeView = view.findViewById(R.id.fragment_repay_view_service_fee);
        serviceFeeLine = view.findViewById(R.id.fragment_repay_line_service_fee);

        initLicense(
                view.findViewById(R.id.fragment_repay_view_license),
                view.findViewById(R.id.fragment_repay_license_logo),
                view.findViewById(R.id.fragment_repay_license_company_name)
        );

        view.findViewById(R.id.fragment_repay_btn_query_process).setOnClickListener(v -> HttpManager_rice.getInstance().requestUserProcess());
        view.findViewById(R.id.fragment_repay_btn_repay).setOnClickListener(v -> {
            HttpManager_rice.getInstance().saveUserBuriedPointAndUpload((this.getClass().getSimpleName()+"_CLICK").toUpperCase());

            switch (StaticConfig_rice.Select_Country){
                case Constant_rice.Country_Cote:
                case Constant_rice.Country_Ghana:
                case Constant_rice.Country_Tanzania:
                case Constant_rice.Country_Senegal:
                    HttpManager_rice.getInstance().orderRepayH5_gh_cl_vi();
                    break;
                case Constant_rice.Country_Uganda:
                case Constant_rice.Country_Kenya:
                    HttpManager_rice.getInstance().orderRepayH5_ug_ke();
                    break;
                case Constant_rice.Country_Nigeria:
                    HttpManager_rice.getInstance().bankList_niri();
                    break;
            }
        });

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        HttpManager_rice.getInstance().getRepayDetail();
    }

    @SuppressLint("SetTextI18n")
    public void updateRepayDetail(){

        //货币单位
        String moneyUnit = StringUtil_rice.getSafeString(StaticConfig_rice.getMoneyUnit(RepayInfo_rice.getInstance().getCurrency())).toUpperCase();

        totalRepaymentTv.setText(moneyUnit+" "+ RepayInfo_rice.getInstance().getRepayAmount());
        dueDateTv.setText(RepayInfo_rice.getInstance().getRepayDate());
        loanAmountTv.setText(RepayInfo_rice.getInstance().getAmount()+" "+moneyUnit);
        interestTv.setText(RepayInfo_rice.getInstance().getInterest()+" "+moneyUnit);
        overdueDaysTv.setText(RepayInfo_rice.getInstance().getOverdueDay()+" "+getString(R.string.word_days));
        overduePenaltyTv.setText(RepayInfo_rice.getInstance().getOverdueAmount()+" "+moneyUnit);

        //判断是否逾期
        if (RepayInfo_rice.getInstance().isOverdue()){
            isOverdue = true;
            overdueDaysTv.setTextColor(Color.RED);
        }else {
            isOverdue = false;
            overdueDaysTv.setTextColor(requireContext().getColor(R.color.blue));
        }

        double serviceFee = RepayInfo_rice.getInstance().getServiceFee();

        switch (StaticConfig_rice.Select_Country){
            case Constant_rice.Country_Cote:
            case Constant_rice.Country_Senegal:
                serviceFee = RepayInfo_rice.getInstance().getRepayProFee();
                HttpManager_rice.getInstance().fetchBankAccount();
                break;
        }

        if (serviceFee<=0){
            serviceFeeView.setVisibility(GONE);
            serviceFeeLine.setVisibility(GONE);
        }else {
            serviceFeeView.setVisibility(VISIBLE);
            serviceFeeLine.setVisibility(VISIBLE);
            serviceFeeTv.setText(serviceFee + " " + moneyUnit);
        }

        String notice = AppShowInfo_rice.getInstance().getRepayNotice();
        if (repayNoticeFlag && !TextUtils.isEmpty(notice)){
            repayNoticeFlag = false;
            new NoticeDialog_rice(requireContext(),notice).show();
        }

    }

    public void onFetchBankListResponse_Niri(){}

}